import {observable,action,computed} from 'mobx';
import { message } from 'antd';
import * as goalMain from '../../apis/goal/goalMain'
import * as goalCommonAPI from '../../apis/goal/goalCommon'
import * as goalBaseAPI from '../../apis/goal/goalBase'
import {WeaLocaleProvider} from 'ecCom';
import {WeaTableNew } from 'comsMobx';
const { TableStore } = WeaTableNew;
const getLabel = WeaLocaleProvider.getLabel;
export default class goalMainStore{

    @observable loading = false; // 页面初始化的loading状态：数据加载成功前后前使用
    @observable yeardata = []; //年集合
    @observable nowyear = "";//当前年
    @observable monthdata = [];//月集合
    @observable nowmonth = "";//当前月
    @observable seasondata = [];//季度集合
    @observable nowseason = "";  //当前季度
    @observable sortcond = 3;//视图分类 1-列表 3-分类 4-层级
    @observable condtype = 1; //菜单分类 1-我的目标
    @observable currentperiod = "3";//当前周期 3-年度
    @observable currentstatus = "1";//当前状态 1-进行中
    @observable datalist = []; //列表数据集合
    @observable optparams = {};//ViewTree组件操作数组
    localtime = new Date().getTime();
    @observable refreshlist = true;//用于刷新viewtree
    @observable hasmore =false;//列表模式下是否有更多
    pagenum = 1;//列表模式当前页
    itotal = 1;//列表模式总条数
    pagesize = 20; //列表每页页数
    @observable canCreate = false;
    @observable isCompanyPerson = false; // 是否为公司目标负责人
    @observable datetype=[];//分类视图类型数组
    excludeids = "";//新增加的目标id
    @observable addname = ""; //列表模式下快速创建文本框的value
    @observable searchname = "";//搜索框 任务名称
    @observable orderby = "";//搜索框 排序字段
    @observable orderbytype = "desc";//搜索框 排序方式
    @observable principalid = [];//搜索框 责任人
    @observable enddate = "";//搜索框 结束时间
    @observable enddate1 = "";//搜索框 结束时间
    @observable refreshDetail = false;//用于刷新详细
    @observable isShowType = true; // 用于判断分类列表是否展示数据
    @observable principaler = {}; //责任人对象
    @observable tag= "";//搜索框 标签
    @observable dropdisplay = true;//批量删除按钮是否置灰 false 否 true 是
    @observable checkedIds = '';
    @observable allCheckBoxVal = "0"; //是否选中全选

    @observable orgCardList = []; //组织卡片
    @observable typeId = ""; //目标类型 1公司目标 2分部目标 3部门目标
    @observable orgId = ""; //组织id
    @observable orgtitle = "";//标题
    @observable currentOrgId  = "0";//当前组织选项
    @observable orgRight = 0; //当前组织目标权限 0 无权限 1 可查看 2 可创建 
    @observable fieldOptions = []; //导出选项字段

    @action
    setTypeId = (data) =>{
        this.typeId = data;
    } 

    @action
    setOrgId = (data) =>{
        this.orgId = data;
    }

    @action
    setOrgtitle = (data) =>{
        this.orgtitle = data;
    }

    @action
    setOrgRight = (data) =>{
        this.orgRight = data;
    }

    @action
    setCanCreate = (data) =>{
        this.canCreate = data;
    }


    //获取目标集合
    @action
    getGoalList = (params) => {
        if (params.menu && params.menu == 1 && this.sortcond == 4) {
            this.sortcond = 1;
        }
        this.getCommonParams(params);
        this.excludeids = "";
        this.allCheckBoxVal = "0"
        if (this.sortcond == "1" && this.dataShowType == "2") {
            this.getTableListData(params)
        }else{
            this.loading = true;
            goalMain.getGoalList(params).then(action(res => {
                if (res.status) {
                    if (this.sortcond == 1) {
                        this.hasmore = res.hasmore;
                        this.itotal = res.total;
                        this.pagenum = 1;
                    }
                    this.canCreate = res.canCreate;
                    this.isCompanyPerson = res.isCompanyPerson;
                    this.datalist = res.datas;
                    this.datetype = res.datetype;
                    this.principaler = res.principaler;
                }
                this.loading = false;
            }));
        }
    }
    //更多列表
    @action
    getGoalMoreList = (params) =>{
        this.loading = true;
        this.getCommonParams(params);
        params.excludeids = this.excludeids;
        goalMain.getMoreGoalList(params).then(action(res => {
            if(res.status){
              if(this.sortcond==1){
                this.pagenum = params.pagenum;
                this.hasmore = res.hasmore;
              }else{
                 this.datetype[params.index].pagenum = params.pagenum;
                 this.datetype[params.index].hasmore = res.hasmore;
              }
              this.datalist = this.datalist.concat(res.datas);
            }
            this.loading = false;
        }));
    }
    //得到年 月 季度下拉框集合
    @action
    getDateList = () =>{
        let now = new Date(); //当前日期
        this.nowmonth = (now.getMonth()+1)+""; //当前月
        this.nowyear = now.getFullYear()+""; //当前年
        this.seasondata = [];
        this.yeardata = [];
        this.monthdata = [];
        for(let i= parseInt(this.nowyear)+3;i>parseInt(this.nowyear)-6;i--){
            if(i==this.nowyear){
                this.yeardata.push({key: i+"",showname: i,selected: true});
            }else{
                this.yeardata.push({key: i+"",showname: i,selected: false});
            }
        }
        for(let i=1;i<13;i++){
            if(i==this.nowmonth){
                this.monthdata.push({key: i+"",showname: i+getLabel(33452,'月'),selected: true});
            }else{
                this.monthdata.push({key: i+"",showname: i+getLabel(33452,'月'),selected: false});
            }
        }
  
        if(this.nowmonth<4){
            this.nowseason = "1";
        }else if(this.nowmonth<7){
            this.nowseason = "2";
        }else if(this.nowmonth<10){
            this.nowseason ="3";
        }else{
            this.nowseason = "4";
        }
        for(let i=1;i<5;i++){
            if(i==this.nowseason){
                this.seasondata.push({key: i+"",showname: i+getLabel(17495,'季度'),selected: true});
            }else{
                this.seasondata.push({key: i+"",showname: i+getLabel(17495,'季度'),selected: false});
            }
        }
    }
    //修改目标状态
    @action
    toChangeStatus = params => {
        goalMain.toChangeStatus(params).then(action((data) => {
            if(data.status){
                this.refreshlist = false;
                    if(params.status==4 || (this.currentstatus == '1' && params.status != 1) || (this.currentstatus == '2' && params.status != 2)){
                        this.optparams = {};
                        this.optparams.opttype = "del";
                        this.optparams.optkey = params.goalId;
                    }else{  
                        let comsbtn = params.comsbtn;
                        let arr = {};
                        if(params.status==1){
                            arr.title = getLabel(508558,'当前状态未完成,点击设置完成') ;
                            arr.com = getLabel(382564,'标记完成') ;
                            arr.type = "complete";
                            arr.icon = "check";
                        }else{
                            arr.title = getLabel(508575,'当前状态完成,点击设置未完成');
                            arr.com = getLabel(382565,'标记进行');
                            arr.type = "progress";
                            arr.icon = "check-circle-o";
                        }
                        comsbtn.map((element,idx)=>{
                            if(element.type==params.type){
                                comsbtn[idx] = arr;
                            }
                        });
                        this.optparams = {};
                        this.optparams.comsbtn = comsbtn;
                        this.optparams.opttype = "update";
                        this.optparams.optkey = params.goalId;
                        this.optparams.status = params.status;
                    }
                    this.localtime = this.getLocalTime();
                    this.refreshlist = true;
                    this.refreshDetail = true;
                    if(typeof params.doback === "function"){
                        params.doback();
                    }
            }
        }));
    }
    //添加或取消关注
    @action
    setSpecial = params => {
        goalMain.setSpecial(params).then(action((data) => {
            if(data.status){
                this.refreshlist = false;
                let comsbtn = params.comsbtn;
                let arr = {};
                if(params.special==1){
                    arr.title = getLabel(26938,'取消关注');
                    arr.com = getLabel(26938,'取消关注');
                    arr.type = "canclespecial";
                    arr.icon = "star";
                }else{
                    arr.title = getLabel(26939,'添加关注');
                    arr.com = getLabel(25436,'关注');
                    arr.type = "special";
                    arr.icon = "star-o";
                }
                comsbtn.map((element,idx)=>{
                    if(element.type==params.type){
                        comsbtn[idx] = arr;
                    }
                });
                this.optparams = {};
                this.optparams.comsbtn = comsbtn;
                this.optparams.opttype = "update";
                this.optparams.optkey = params.goalId;
                this.localtime = this.getLocalTime();
                this.refreshlist = true;
                this.refreshDetail = true;
                if(typeof params.doback === "function"){
                    params.doback();
                }
            }
        }));
    }
    //快速新建目标
    @action
    addGoal = params => {
        params.sorttype = this.sortcond;
        goalMain.addGoal(params).then(action((data) => {
            if(data.status){
                this.refreshlist = false;
                this.optparams = {};
                this.optparams.datas = data.datas;
                this.optparams.opttype = "add";
                this.optparams.optkey = data.goalId;
                this.excludeids += data.goalId+",";
                this.localtime = this.getLocalTime();
                if(params.sorttype==1){
                    this.addname = "";
                }else if(params.sorttype==3){
                    this.datetype[params.index].hasdata = "1";
                    this.datetype[params.index].inputval = "";
                }
                this.refreshlist = true;
                if (typeof params.doback === "function") {
                    params.doback(data.goalId)
                }
            }
        }));
    }

    //dialog新建目标返回
    @action
    setNewGoalInfo(params){
        this.refreshlist = false;
        this.optparams = params;
        this.localtime = this.getLocalTime();
        if(params.opttype == 'add' || params.opttype == 'dialogAdd'){
            this.excludeids += params.optkey +",";
            if(params.optdatetype && this.datetype && this.datetype.length>0){
                for(let i=0;i<this.datetype.length;i++){
                    if(this.datetype[i].id == params.optdatetype){
                        this.datetype[i].hasdata = "1";
                    }
                }
            }
        }
        if(params.opttype == 'del'){
            if(this.datetype.length == 0){
                this.isShowType = false;
            }
        }
        this.refreshlist = true;
    }


    @action
    getLocalTime(){
        return new Date().getTime();
    }
    @action
    getCommonParams(params){
        params.sorttype = this.sortcond;
        params.condtype = this.condtype;
        params.period = this.currentperiod;
        params.status = this.currentstatus;
        params.periody = this.nowyear;
        params.periodm = this.nowmonth;
        params.periods = this.nowseason;
        if(this.sortcond == 1){
            params.pagesize = 20
        }else{
            params.pagesize = 10
        }
        // params.pagesize = this.pagesize;
        params.searchname = this.searchname;
        params.orderby = this.orderby;
        params.orderbytype = this.orderbytype;
        if(this.principalid && this.principalid.length>0){
            params.principalid = this.principalid[0].id;
        }
        if(this.tag && this.tag.length>0){
            // params.tag = this.tag[0].id;
            params.tag = this.tag;
        }
        params.enddate = this.enddate;
        params.enddate1 = this.enddate1;
        params.typeId = this.typeId;
        params.orgId = this.orgId;
    }

    @action
    setDataList(params){
        this.datalist.map((element)=>{
            if(element.key == params.key){
                element.rightData[0].name = params.principalid;
                element.rightData[1].name = params.enddate;
                element.rightData[2].name = params.feedbackcount;
            }
        })
    }

    @action("标签处理")
    editGoalTag = params =>{
        this.loading = true;
        goalCommonAPI.editGoalField(params).then(action((data) => {
            if(data.status && params.doback){
                params.doback(data.status);
            }
            if(params.tagtype=="2" || params.tagtype=="3"){
                this.getGoalList({});
            }
            this.loading = false;
        }))
    }

    @action("批量删除")
    batchDelGoal = params =>{
        this.loading = true;
        params.operation = "batchDel";
        goalCommonAPI.editStatus(params).then(action((data) => {
            this.getGoalList({});
            if(params.doback){
                params.doback(data.status);
            }
            this.loading = false;
        }))
    }

    @action("重置dropdisplay")
    setDropDisplay(isSelect){
        if(isSelect){
            this.dropdisplay = false;
        }else{
            this.dropdisplay = true;
        }
    }

    @action("设置全选CheckBox框的值")
    setAllCheckBoxValue(v,currentid){
        if(currentid && currentid!=""){
            this.datetype && this.datetype.length>0 &&  this.datetype.map((element,idx)=>{
                if(element.id == currentid){
                    element.allCheckBoxVal = v;
                }
            })
        }else{
            this.allCheckBoxVal = v;
        }
    }

    @action("重置搜索条件")
    resetSearchOption(){
        this.searchname = "";
        this.orderby = "";
        this.orderbytype = "desc";
        this.principalid = [];
        this.enddate = "";
        this.enddate1 = "";
        // this.tag= "";
    }

    @action
    goalCard = (params) => {  //获取组织选项
        this.loading = true;
        this.orgCardList = [];
        this.currentOrgId = "0"
        goalCommonAPI.goalCard(params).then(action(res => {
            if (res.status) {
                this.orgCardList = res.list;
                this.initOrgGoal();
                if (typeof params.doback === "function") {
                    params.doback();
                } else {
                    this.loading = false;
                }
            } else if (!res.api_status) {
                message.error(res.msg || getLabel('25009', '失败'));
            }

        }));
    }

    @action
    initOrgGoal = () =>{
        if(this.orgCardList && this.orgCardList.length > 0) {
            if(this.orgCardList[this.currentOrgId].canCreate){
                this.setOrgRight(2)
            }
            
            this.setOrgtitle(this.orgCardList[this.currentOrgId].orgName)
            this.setTypeId(this.orgCardList[this.currentOrgId].type)
            this.setOrgId(this.orgCardList[this.currentOrgId].orgId)
        }
    }

    @computed
    get orgTypeList(){
        let res = [{
            key: 'orgType',
            label: getLabel(376,'组织'),
            options:[],
        }];
        if(this.orgCardList.length > 0){
            this.orgCardList.map((d,i)=>{
                res[0].options.push({
                    key:""+i,
                    showname:d.orgName,
                })
            });
        }else{
            res = [];
        }
        return res;
    }

    isBatchDel = true    // 是否处于批量删除

    @observable
    dataShowType = "1"   // 1 -- 分类展示   2 -- 表格展示
    
    @action
    changeDataShowType = (dataShowType) => {
        this.dataShowType = dataShowType
    }

    @observable
    tableStore = new TableStore()

    @observable
    refreshTable = false;

    @action
    getTableListData = (params = {}) => {
        this.loading = false;
        params.sorttype = ""
        params.isBatchDel = this.isBatchDel ? "1" : "0"
        this.refreshTable = false;
        goalBaseAPI.goaltableList(params).then(res => {
            if (res.status) {
                this.refreshTable = true;
                this.tableStore.getDatas(res.datas, params.isReloadCurrent ? this.tableStore.current : 1)
            }

        })
    }

    @observable
    tableSelectedRowKeys = [] 


    @action
    setSortCond = (v) =>{
        this.sortcond = v;
        if(v == 3 || v == 4){
            this.changeDataShowType("1")
        }
    }

}